//
//  MTHudLoadingView.swift
//  MT
//
//  Created by zhang qiang on 2018/12/2.
//  Copyright © 2019年 张强. All rights reserved.
//

import Foundation
import UIKit
import Then
import SnapKit

public class MTHudLoadingView : UIView {
    
    private let contentView = UIView().then {
        $0.backgroundColor = UIColor(hex: 0xFFFFFF)
        $0.layer.cornerRadius = 10
        $0.layer.shadowColor = UIColor(hex: 0x140000, alpha: 0.1).cgColor
        $0.layer.shadowOffset = CGSize(width: 0, height: 2)
        $0.layer.shadowOpacity = 0.5
        $0.layer.shadowRadius = 15
    }
    
    private let imageView = UIImageView().then{
        var images : [UIImage] = [UIImage]()
        for i in 1...20 {
            let imageName = "circleLoading\(i)"
            images.append(UIImage.imagePath(named: imageName, bundle: Bundle.MTCategroy)!)
        }
        $0.contentMode = .center
        $0.backgroundColor = UIColor.init(hex: 0xFFFFFF, alpha: 0.95)
        $0.animationImages = images
        $0.animationDuration = 1
        $0.animationRepeatCount = 0
        $0.layer.cornerRadius = 10.0
    }
    
    private let tipLbl = UILabel().then {
        $0.text = "加载中…"
        $0.textColor = UIColor(hex: 0x202932)
        $0.font = PingFangSCRegularFont(14)
        $0.textAlignment = .center
    }
    
    
    public init() {
        super.init(frame: CGRect.zero)
        self.backgroundColor = UIColor.clear
        
        addSubview(contentView)
        
        contentView.addSubview(imageView)
        contentView.addSubview(tipLbl)
        
        contentView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
            make.width.equalTo(120.0)
            make.height.equalTo(120.0)
        }
        
        imageView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 56, height: 56))
            make.top.equalToSuperview().offset(16)
        }
        
        tipLbl.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(imageView.snp.bottom).offset(14)
        }
        
    }
    
    override public func didMoveToSuperview() {
        if let _ =  superview{
            imageView.startAnimating()
        }else{
            imageView.stopAnimating()
        }
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
